package com.help.knowhelps.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.help.knowhelps.R;

import android.R.color;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class UserActivity extends Activity{
	private ViewPager mPager;//页卡内容
    private List<View> listViews; // Tab页面列表
    private ImageView cursor;// 动画图片
    private ImageView page_self, page_friend;// 页卡头标
    private int offset = 0;// 动画图片偏移量
    private int currIndex = 0;// 当前页卡编号
    private int bmpW;// 动画图片宽度
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.user_activity);
		initComponent();
	}
	private void initComponent()
	{
		UiUtils.InitActionBar(this, "", UiUtils.ACTIONBAR_BUTTON_DEFAULT, 
				UiUtils.ACTIONBAR_BUTTON_DISAPPEAR ,clicklistener);
		InitImageView();
		InitViewPager();
		InitTextView();
//		findViewById(R.id.main_imageView_left)
//		bt_layout = (LinearLayout) findViewById(R.id.checkcpu_buttonlayout);
//		ll_wlistguide_above = (LinearLayout) findViewById(R.id.checkcpu_masking_above);
//		ll_wlistguide_below = (LinearLayout) findViewById(R.id.checkcpu_masking_below);
//		ll_wlistguide_above.setOnClickListener(listener);
	}
	/**
     * 初始化动画
     */
    private void InitImageView() {
    	cursor =  (ImageView) findViewById(R.id.user_cursor);
        bmpW =BitmapFactory.decodeResource(getResources(), R.drawable.icon_111).getWidth();// 获取图片宽度
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenW = dm.widthPixels;// 获取分辨率宽度
        offset = (screenW / 2 - bmpW) / 2;// 计算偏移量
        Matrix matrix = new Matrix();
        matrix.postTranslate(offset, 0);
        cursor.setImageMatrix(matrix);// 设置动画初始位置
    }
	/**
	 * 初始化ViewPager
	 */
	@SuppressWarnings("static-access")
	private void InitViewPager() {
		mPager = (ViewPager) findViewById(R.id.user_vPager);
		listViews = new ArrayList<View>();
		LayoutInflater mInflater = getLayoutInflater();
		//test 1 ,---------------------------------------------
		ListView list_help = new ListView(this);
		List<Map<String,String>> date = new ArrayList<Map<String,String>>();
		for(int i=0;i<10;i++)
		{
			Map<String,String> map = new HashMap<String, String>();
			map.put("time", new DateFormat().format("yyyy.mm.dd", new Date()).toString());
			map.put("entity", "请求帮助:"+i);
			map.put("location", "北京"+i);
			map.put("count", i+"");
			date.add(map);
		}
		list_help.setAdapter(new SimpleAdapter(this,date, R.layout.item_help_listview, new String[]{"time","entity","location","count"}, 
				new int[]{R.id.item_help_time,R.id.item_help_entity,R.id.item_help_textView_location,R.id.item_help_textView_discuss}));
		listViews.add(list_help);//mInflater.inflate(R.layout.item_help, null));
		//test 2 ,---------------------------------------------
		List<Map<String,String>> date2 = new ArrayList<Map<String,String>>();
		ListView list_help2 = new ListView(this);
		for(int i=0;i<10;i++)
		{
			Map<String,String> map = new HashMap<String, String>();
			map.put("time", new DateFormat().format("yyyy.mm.dd", new Date()).toString());
			map.put("entity", "参与帮助:"+i);
			map.put("location", "北京"+i);
			map.put("count", i+"");
			date2.add(map);
		}
		list_help2.setAdapter(new SimpleAdapter(this,date2, R.layout.item_help_listview, new String[]{"time","entity","location","count"}, 
				new int[]{R.id.item_help_time,R.id.item_help_entity,R.id.item_help_textView_location,R.id.item_help_textView_discuss}));
		listViews.add(list_help2);
		//--------------------------------------------------------------------------------------------------以上为测试数据
		// do...        
		mPager.setAdapter(new mPagerAdapter(listViews));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener());
	}
	/**
     * 初始化头标
     */
    private void InitTextView() {
        page_self = (ImageView) findViewById(R.id.user_imagetext_page1);
        page_friend = (ImageView) findViewById(R.id.user_imagetext_page2);

        page_self.setOnClickListener(new MyOnClickListener(0));
        page_friend.setOnClickListener(new MyOnClickListener(1));
    }
    /**
     * 页卡切换监听
*/
    public class MyOnPageChangeListener implements OnPageChangeListener {

        int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量
        int two = one * 2;// 页卡1 -> 页卡3 偏移量

        @Override
        public void onPageSelected(int arg0) {
            Animation animation = null;
            switch (arg0) {
            case 0:
                if (currIndex == 1) {
                    animation = new TranslateAnimation(one, 0, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, 0, 0, 0);
                }
                break;
            case 1:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(offset, one, 0, 0);
                } else if (currIndex == 2) {
                    animation = new TranslateAnimation(two, one, 0, 0);
                }
                break;
            case 2:
                if (currIndex == 0) {
                    animation = new TranslateAnimation(offset, two, 0, 0);
                } else if (currIndex == 1) {
                    animation = new TranslateAnimation(one, two, 0, 0);
                }
                break;
            }
            currIndex = arg0;
            animation.setFillAfter(true);// True:图片停在动画结束位置
            animation.setDuration(300);
            cursor.startAnimation(animation);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
    /**
     * 头标点击监听
*/
    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;
        public MyOnClickListener(int i) {
            index = i;
        }
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    };
	private OnClickListener clicklistener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.actionbar_imageView_left:
				finish();
				break;
			default:
				break;
			}
		}
	};
}
